package com.sita.linboard.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.sita.linboard.R;
import com.sita.linboard.base.ActivityCollector;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.Constants;
import com.sita.linboard.utils.ToastUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/8/29.
 *  //HXID:439435360729041973
 *
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private EditText phone,pass;
    private RegisterBackBean bean;
    private Button confirm_bt;
    private TextView head;
    private LinearLayout back;
    @Override
    protected void initData() {
        bean = new RegisterBackBean();
        confirm_bt.setOnClickListener(this);
        head.setText("注册");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        phone = bindView(R.id.user_phone);
        pass = bindView(R.id.user_pass);
        confirm_bt = bindView(R.id.confirm);
        head = bindView(R.id.head_title);
        head.setVisibility(View.VISIBLE);
        back = (LinearLayout) findViewById(R.id.img_back);
        back.setOnClickListener(this);
        back.setVisibility(View.VISIBLE);
    }

    @Override
    protected int getId() {
        ActivityCollector.addActivity(this);
        return R.layout.activity_register;
    }
    /**
     * 用户注册
     */
    private void getRegister(){
        final RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.mobile = phone.getText().toString();
        registerRequest.password = pass.getText().toString();
        RestClient.getRestService().register(registerRequest, new Callback<RegisterBackBean>() {

            @Override
            public void success(RegisterBackBean registerBackBean, Response response) {
                if (response.getStatus()==200){
                    bean = registerBackBean;
                    DriverLoginActivity.DriverLoginIntent(RegisterActivity.this);
                    finish();
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.confirm:
                if (isPhoneNumberValid(phone.getText().toString())){
                    getRegister();
                }
                else {
                    ToastUtils.showLong("请确认是否为手机号码");
                }

                break;
            case R.id.img_back:
                finish();
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    public static void RegisterIntent(Context context){
        Intent intent = new Intent(context,RegisterActivity.class);
        context.startActivity(intent);
    }

    /**
     * 判断电话号码是否为手机号码
     * @param phoneNumber    手机号
     * @return
     */
    public static boolean isPhoneNumberValid(String phoneNumber) {
        boolean isValid = false;
	   /*
	    * 可接受的电话格式有：
	    */
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
	   /*
	    * 可接受的电话格式有：
	    */
        String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);

        Pattern pattern2 = Pattern.compile(expression2);
        Matcher matcher2 = pattern2.matcher(inputStr);
        if(matcher.matches() || matcher2.matches()) {
            isValid = true;
        }
        return isValid;
    }
}
